package com.relytosoft.mizar.media.camera.services.utils;

import cn.hutool.core.util.StrUtil;
import com.relytosoft.mizar.media.system.api.pojo.dto.DistrictFeignDto;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lj
 * @date 2022/4/21 10:00
 */

public class FeignInSystemUtils {
    /**
     * 根据区域id 查询所有子id
     *
     * @param id      区域ID
     * @param result  最总结果
     * @param allArea 所有区域信息
     * @return 子级ID
     */
    public static List<Integer> findAreaAllChildren(Integer id, List<Integer> result, List<DistrictFeignDto> allArea) {
        result.add(id);
        for (DistrictFeignDto districtFeignDTO : allArea) {
            if (id.equals(districtFeignDTO.getParentId())) {
                findAreaAllChildren(districtFeignDTO.getId(), result, allArea);
            }
        }
        return result;
    }

    /**
     * 根据区域ID查询完整路径
     *
     * @param districtId 区域id
     * @param allArea    全部区域数据
     * @return 完整路径
     */
    public static String completeRoute(Integer districtId, List<DistrictFeignDto> allArea) {
        if (allArea == null) {
            return null;
        }
        //1. 查询区域信息
        DistrictFeignDto districtFeignDTO = allArea.stream()
                .filter(item -> item.getId().equals(districtId))
                .collect(Collectors.toList()).get(0);
        //2. 获取完整路径
        String path = districtFeignDTO.getDepthPath();
        if (StrUtil.isEmptyIfStr(path)) {
            return districtFeignDTO.getName();
        }
        //3. 分割
        String[] split = path.split(",");
        StringBuilder resultPath = new StringBuilder();
        //4. 遍历数据
        for (String s : split) {
            List<DistrictFeignDto> districtFeignDTOList = allArea.stream()
                    .filter(item -> item.getId().equals(Integer.parseInt(s)))
                    .collect(Collectors.toList());
            if (districtFeignDTOList.size() <= 0) {
                continue;
            }
            resultPath.append(districtFeignDTOList.get(0).getName()).append("/");
        }
        resultPath.append(districtFeignDTO.getName()).append("/");
        if (resultPath.length() > 0) {
            return resultPath.deleteCharAt(resultPath.length() - 1).toString();
        } else {
            return resultPath.toString();
        }
    }
}
